2. ECMAScript - Overview 概述


Posted by nosleepengineer on 2023-09-17

本文提及的,是 ECMAScript 中非規範性的概述,不是 JavaScript 的標準的一部份,參考的內容來自規範中的第四章。ECMAScript 是一個腳本語言,必須依賴主機環境才能執行,所以除了語言本身定義的語法與屬性、方法等等外,還能使用主機環境中定義的屬性與方法。下面會針對幾個概念說明。

腳本語言 (scripting language)

A scripting language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. In such systems, useful functionality is already available through a user interface, and the scripting language is a mechanism for exposing that functionality to program control.

根據規範中的定義,腳本語言是一種操作、客制化和自動化現有系統的程式語言。通過腳本語言操作的系統,稱為腳本語言的主機環境 (host environment)。主機環境通常提供了一些功能,腳本語言是一個將這些功能通過程式控制的工具。腳本語言的作用範圍無法超越主機環境,像是直接管理電腦記憶體、從主機環境外輸入或對外輸出資訊、和主機環境外的其它應用程式互動等等,這類型的任務都必須藉由主機環境來完成。

主機環境 (host environment)

A host environment is a particular choice of definition for all host-defined facilities. A host environment typically includes objects or functions which allow obtaining input and providing output as host-defined properties of the global object.

web 瀏覽器

對前端工程師來說,最熟悉的主機環境就是 web 瀏覽器,web 瀏覽器提供了一系列的 API 讓 JavaScript 進行操作,例如 debug 常用的 Console、發出請求用的 XMLHttpRequest、Fetch、 操作 DOM 元素的 Document 等等。這些 API 並不是由 ECMAScript 規範所定義的,如果要更進一步了解瀏覽器提供了哪些 API 及其使用方式,可以在 MDN 中查詢。

其他主機環境

除了瀏覽器以外,還有其他的主機環境能支持 JavaScript 運行。例如 node.js,相對於瀏覽器提供的全域物件 Window,node.js 提供了 global 作為它的全域物件。如果你的電腦有安裝 node,你就能在 terminal 中輸入 node 開啟 node 環境,並輸入 global 查看這個全域物件。

另外,Microsoft Windows 也有提供可作為 JavaScript 的主機環境 Windows Script Host ,JavaScript 可以透過它所定義的 wscript 全域物件和電腦互動。只要把下面的程式碼存成 .js 檔,在 Windows Script Host 環境執行電腦就能跳出提示視窗。

WScript.echo("Hello world!");

主機和實現 (Hosts and Implementations)

A host is an external source that further defines facilities listed in Annex D but does not further define other implementation-defined or implementation-approximated facilities. In informal use, a host refers to the set of all implementations, such as the set of all web browsers, that interface with this specification in the same way via Annex D. A host is often an external specification, such as WHATWG HTML (https://html.spec.whatwg.org/). In other words, facilities that are host-defined are often further defined in external specifications.

An implementation is an external source that further defines facilities enumerated in Annex D or those that are marked as implementation-defined or implementation-approximated. In informal use, an implementation refers to a concrete artefact, such as a particular web browser.

an implementation-approximated facility is defined in whole or in part by an external source but has a recommended, ideal behaviour in this specification

an implementation-defined facility is defined in whole or in part by an external source to this specification

為了讓 ECMAScript 規範能夠更好的和主機環境整合,規範中將一些機制交由外部的主機或實現來定義,像是某些抽象操作 (abstract operations,基本上就是 JavaScript 中的方法的概念,也就是函數,不過抽象操作只存在規範中。規範里也有很多重覆出現的操作,為了方便,規範將這樣的方法抽象化定義為一個抽象操作,就像 JavaScript 中的陣列方法)。主機在規範中的定義是提供了附件 D 中提到的機制的外部來源,非正式的用法中指的是實現的集合。而實現是提供了附件 D 中提到被標記為 implementation-approximated 或 implementation-defined 的機制的外部來源,但在非正式的用法中指的是特定的外部系統,如 Chrome 瀏覽器。所以大致上可以把規範中提到主機的意思理解為瀏覽器的集合,實現則是一個一個具體的瀏覽器

implementation-approximated 和 implementation-defined 的概念很難用易懂的方式翻譯,但簡單來說他們都是交由外部來源定義的機制,但規範中有提出建議的實現方式的機制的稱為 implementation-approximated,例如在規範中定義的 Math.exp。

ECMAScript 概述

ECMAScript 是一個以物件為基礎的語言,基本語言和主機提供的功能都是物件的形式。物件是由零到多個屬性的集合,並且,每個屬性都有自己的屬性描述器 (Property Descriptor) 來定義屬性可以如何被操作。屬性則是保存其他物件、基礎值或函數的容器。ECMAScript 規範中定義了內建物件的集合和一組內建運算子,模組化的方法和嚴格模式。這些內容都會在後續的章節中詳細說明。

結語

本章主要是對 ECMAScript 做一個整體的描述,給出一些術語及定義的說明,這部份在後面的章節都會詳細的描述,因此這邊就跳過了。簡單來說,JavaScript 是一個在特定的環境中才能執行的程式語言,而這個語言基本上由物件構成,語言本身有內建的物件,主機環境也有提供。以瀏覽器為例,它提供了一個包含各種屬性和方法的 Window 物件,關於 Window 的一切就不在 ECMAScript 中定義了。我們在這個規範中能看到的,就只有規範本身定義的部份。










Related Posts

[Day 05] 走訪器模式,建造者模式,責任鏈模式,解譯器模式

[Day 05] 走訪器模式,建造者模式,責任鏈模式,解譯器模式

金魚系列,排板篇

金魚系列,排板篇

UI教務處|UI/UX核心觀念|用Figma打造絕佳UI/UX

UI教務處|UI/UX核心觀念|用Figma打造絕佳UI/UX


Comments